<?php

namespace app\web\controller;
use app\web\model\GoodPintuansModel;
use app\web\model\GoodsTypeModel;
use app\web\model\StoreGoodsTypeModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 抽奖交友团列表
 */
class LuckyDrawController extends StoreBaseController
{
 	protected $model;
	protected $goodsType_model;
	protected $storeGoodsType_model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new GoodPintuansModel();
		$this->goodsType_model = new GoodsTypeModel();
		$this->storeGoodsType_model = new StoreGoodsTypeModel();
		
		//商品类目
		$goodsType_list = $this->goodsType_model->select();
		//商品分类
		$storeGoodsType_list = $this->storeGoodsType_model->where('agent_id',session('storeInfo')['id'])->select();		
		$this->assign('storeGoodsType_list',$storeGoodsType_list);
		$this->assign('goodsType_list',$goodsType_list);		
    }
	
	
    /**
     * 商品列表
	 * @param int $page
     */
    public function index($page = 1){
    	$goods_state = request()->param('goods_state');		
    	$store_id = session('storeInfo')['id'];
    	$list = $this->model->where('agent_id',$store_id)->where('goods_state','2')->where('is_active','1')->paginate(10);		
		if(request()->isPost()){
		 	$param = input('post.');
			$goodsname = '%'.$param['goodsname'].'%';
			$list = $this->model->where('name', 'like', $goodsname)->paginate(10);
		}
		return $this->fetch('index', ['list' => $list,'goods_state'=>$goods_state]);
    }
	
	/**
     *添加抽奖交友团商品
     */
    public function add(){
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			$param['goods_state'] = '2';
			$param['is_active'] = '1';
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('lucky_draw/index',['goods_state'=>'2']));
            }else{
                $this->error('添加失败');
            }
        }
		 return $this->fetch();
    }

    /**
     * [edit 编辑]
     * @return 
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('lucky_draw/index',['goods_state'=>'2']));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
		$this->assign('pid','');
        return $this->fetch();
    }
	
	 /**
	 * [offshelfpt 抽奖交友团上下架]
	 * @return 
	 */
    public function offshelf($id)
    {
    	$param = input('post.');     
     	if ($this->model->allowField(true)->save(['goods_state'=>'1'],['id'=>$id]) !== false) {
            $this->success('修改成功', url('lucky_draw/index',['goods_state'=>'2']));
        } else {
            $this->error('修改失败');
        }
        $id = input('param.id');
		$list = $this->model->where('agent_id',session('storeInfo')['id'])->where('goods_state','2')->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('成功');
        } else {
            $this->error('删除失败');
        }
    }
}